package gsm.cable.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;

public class AdminWidget extends Composite {
	private static final String ACCOUNT_DETAIL = "Account Detail";
	private static final String REGISTER_COMPLAINT = "Register Complaint";
	private static final String COMPLAINT_STATUS = "Complaint Status";
	private static AdminWidgetUiBinder uiBinder = GWT.create(AdminWidgetUiBinder.class);

	@UiField
	ScrollPanel bodyPanel;

	@UiField
	HorizontalPanel menuBar;

	@UiField
	HorizontalPanel subMenuBar;

	interface AdminWidgetUiBinder extends UiBinder<Widget, AdminWidget> {
	}

	public AdminWidget() {
		initWidget(uiBinder.createAndBindUi(this));
		addItem(ACCOUNT_DETAIL);
		addItem(REGISTER_COMPLAINT);
		addItem(COMPLAINT_STATUS);
		setBodyPanelSize();
		Window.addResizeHandler(new ResizeHandler() {

			@Override
			public void onResize(ResizeEvent event) {
				setBodyPanelSize();
			}
		});
	}

	private HTML addItem(String label) {
		HTML menuItem = new HTML(label);
		menuItem.addStyleName("app_inner_item");
		MenuMouseHandlerImpl mouseHandlerImpl = new MenuMouseHandlerImpl();
		menuItem.addMouseOutHandler(mouseHandlerImpl);
		menuItem.addMouseOverHandler(mouseHandlerImpl);
		menuItem.addClickHandler(mouseHandlerImpl);
		menuBar.add(menuItem);
		menuItem.getElement().getParentElement().addClassName("app_menu_item");
		return menuItem;
	}

	private HTML addSubMenuItem(String label) {
		HTML menuItem = new HTML(label);
		menuItem.addStyleName("app_submenu_item");
		SubMenuMouseHandlerImpl mouseHandlerImpl = new SubMenuMouseHandlerImpl();
		menuItem.addMouseOutHandler(mouseHandlerImpl);
		menuItem.addMouseOverHandler(mouseHandlerImpl);
		menuItem.addClickHandler(mouseHandlerImpl);
		subMenuBar.add(menuItem);
		menuItem.getElement().getParentElement().addClassName("app_submenu_inner");
		return menuItem;
	}

	private class MenuMouseHandlerImpl implements MouseOverHandler, MouseOutHandler, ClickHandler {

		@Override
		public void onMouseOut(MouseOutEvent event) {
			((HTML) event.getSource()).removeStyleName("app_item_selected");
			// subMenuBar.clear();
		}

		@Override
		public void onMouseOver(MouseOverEvent event) {
			HTML item = (HTML) event.getSource();
			item.addStyleName("app_item_selected");
		}

		@Override
		public void onClick(ClickEvent event) {
			HTML item = (HTML) event.getSource();
			String label = item.getText();
			subMenuBar.clear();
			// Window.alert(label);
			if (label.equals(ACCOUNT_DETAIL)) {
				item.addStyleName("app_item_selected");
				addSubMenuItem("Acc1");
				addSubMenuItem("Acc2");
			} else if (label.equals(COMPLAINT_STATUS)) {
				item.addStyleName("app_item_selected");
				addSubMenuItem("Comp1");
				addSubMenuItem("Comp2");
			} else if (label.equals(REGISTER_COMPLAINT)) {
				item.addStyleName("app_item_selected");
				addSubMenuItem("Reg1");
				addSubMenuItem("Reg2");
			}
		}

	}

	private class SubMenuMouseHandlerImpl implements MouseOverHandler, MouseOutHandler, ClickHandler {

		@Override
		public void onMouseOut(MouseOutEvent event) {
			((HTML) event.getSource()).removeStyleName("app_submenu_item_selected");
		}

		@Override
		public void onMouseOver(MouseOverEvent event) {
			((HTML) event.getSource()).addStyleName("app_submenu_item_selected");
		}

		@Override
		public void onClick(ClickEvent event) {
//			Window.alert(((HTML) event.getSource()).getText());
			bodyPanel.add(new BodyWidget());
		}

	}

	private void setBodyPanelSize() {
		int height = Window.getClientHeight() - 124;
		int width = Window.getClientWidth() - 17;
		this.bodyPanel.setSize(width + "px", height + "px");
	}

}
